﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Heroius.BlueMeasure
{
    public abstract class RecordMaker
    {
        public abstract bool TakePoint(Point p);

        public abstract MeasureRecord GenRecord(double ZoomScale, MeasureRecord scale = null);

        protected static double GetLen(Point p1, Point p2)
        {
            var x = p1.X - p2.X;
            var y = p1.Y - p2.Y;
            return Math.Sqrt(x * x + y * y);
        }

        protected static Color GenRandomColor()
        {
            return ColorStore[random.Next(0, ColorStore.Length)];
        }

        static Color[] ColorStore = new Color[] { Colors.Red, Colors.Green, Colors.Purple, Colors.Orange, Colors.LightBlue };
        static Random random = new Random();
    }

    public class LineRecordMaker: RecordMaker
    {
        public Point Start { get; set; }
        public Point End { get; set; }

        /// <summary>
        /// 指示绘图阶段是否在绘制第一个点
        /// </summary>
        bool dw_step1 = true;

        public override MeasureRecord GenRecord(double ZoomScale, MeasureRecord scale = null)
        {
            var r = new MeasureRecord();
            r.Length = GetLen(Start, End);
            if (scale != null)
                r.Claim = r.Length * scale.Claim / scale.Length;
            else
                r.Claim = r.Length;
            r.Time = DateTime.Now;
            r.Color = GenRandomColor();
            //图形
            r.Attached = new Line()
            {
                X1 = Start.X,
                X2 = End.X,
                Y1 = Start.Y,
                Y2 = End.Y,
                Stroke = new SolidColorBrush(r.Color),
                StrokeThickness = Configuration.Default.AsisLineWidth * (Configuration.Default.AsisZoom ? ZoomScale : 1)
            };
            return r;
        }

        public override bool TakePoint(Point p)
        {
            if (dw_step1)
            {
                Start = p;
                dw_step1 = false;
                return true;
            }
            else
            {
                End = p;
                return false;
            }
        }
    }

    public class MultiRecordMaker : RecordMaker
    {
        List<Point> points = new List<Point>();
        List<double> lens = new List<double>();

        public override MeasureRecord GenRecord(double ZoomScale, MeasureRecord scale = null)
        {
            var r = new MeasureRecord();
            r.Length = lens.Sum();
            r.Claim = r.Length * scale.Claim / scale.Length; //scale cannot be null for multi
            r.Time = DateTime.Now;
            r.Color = GenRandomColor();
            r.Attached = new Polyline()
            {
                Points = new PointCollection(points),
                Stroke = new SolidColorBrush(r.Color),
                StrokeThickness = Configuration.Default.AsisLineWidth * (Configuration.Default.AsisZoom ? ZoomScale : 1)
            };
            return r;
        }

        public override bool TakePoint(Point p)
        {
            if (points.Count > 0)
            {
                var o = points.Last();
                var l = GetLen(o, p);
                lens.Add(l);
            }
            points.Add(p);
            return true;
        }
    }
}
